package com.ruoyi.background.service.impl;

import com.ruoyi.background.domain.AppGradeRecord;
import com.ruoyi.background.domain.AppWechatUser;
import com.ruoyi.background.domain.AppWorkImg;
import com.ruoyi.background.mapper.AppWorkImgMapper;
import com.ruoyi.background.service.IAppGradeRecordService;
import com.ruoyi.background.service.IAppWechatUserService;
import com.ruoyi.background.service.IAppWorkImgService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 健步风采照片Service业务层处理
 *
 * @author susu
 * @date 2024-09-04
 */
@Service
public class AppWorkImgServiceImpl implements IAppWorkImgService {
    @Autowired
    private AppWorkImgMapper appWorkImgMapper;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private IAppWechatUserService wechatUserService;

    @Autowired
    private IAppGradeRecordService gradeRecordService;

    /**
     * 查询健步风采照片
     *
     * @param id 健步风采照片主键
     * @return 健步风采照片
     */
    @Override
    public AppWorkImg selectAppWorkImgById(Long id) {
        return appWorkImgMapper.selectAppWorkImgById(id);
    }

    /**
     * 查询健步风采照片列表
     *
     * @param appWorkImg 健步风采照片
     * @return 健步风采照片
     */
    @Override
    public List<AppWorkImg> selectAppWorkImgList(AppWorkImg appWorkImg) {
        List<AppWorkImg> workImgList = appWorkImgMapper.selectAppWorkImgList(appWorkImg);
        for (AppWorkImg workImg : workImgList) {
            String images = workImg.getImages();
            if (!ObjectUtils.isEmpty(images)) {
                String[] split = images.split(",");
                workImg.setImagesList(split);
            }
        }
        return workImgList;
    }

    /**
     * 新增健步风采照片
     *
     * @param appWorkImg 健步风采照片
     * @return 结果
     */
    @Override
    public int insertAppWorkImg(AppWorkImg appWorkImg) {
        appWorkImg.setCreateTime(DateUtils.getNowDate());
        appWorkImg.setUserId(SecurityUtils.getUserId());
        appWorkImg.setDeptId(SecurityUtils.getDeptId());
        appWorkImg.setCreateBy(SecurityUtils.getUsername());
        appWorkImg.setUpdateTime(DateUtils.getNowDate());
        appWorkImg.setUpdateBy(SecurityUtils.getUsername());
        appWorkImg.setStatus(1);
        return appWorkImgMapper.insertAppWorkImg(appWorkImg);
    }

    /**
     * 修改健步风采照片
     *
     * @param appWorkImg 健步风采照片
     * @return 结果
     */
    @Override
    public int updateAppWorkImg(AppWorkImg appWorkImg) {
        appWorkImg.setUpdateTime(DateUtils.getNowDate());
        appWorkImg.setUpdateBy(SecurityUtils.getUsername());
        return appWorkImgMapper.updateAppWorkImg(appWorkImg);
    }

    /**
     * 修改状态
     *
     * @param id     id
     * @param status status
     * @return 结果
     */
    @Override
    public int changeStatus(Long id, Integer status) {
        AppWorkImg appWorkImg = this.selectAppWorkImgById(id);
        if (ObjectUtils.isEmpty(appWorkImg)) throw new RuntimeException("对象不存在");
        AppWorkImg update = new AppWorkImg();
        update.setId(id);
        update.setStatus(status);
        update.setUpdateTime(DateUtils.getNowDate());
        update.setUpdateBy(SecurityUtils.getUsername());
        if (status == 0) { //展示 判断是否第一次
            this.isAddWorkImgGrade(appWorkImg);
        }
        return appWorkImgMapper.updateAppWorkImg(update);
    }

    /**
     * 是否发放积分
     *
     * @param workImg 活动风采
     */
    @Async
    @Transactional
    public void isAddWorkImgGrade(AppWorkImg workImg) {
        //查询用户今天是否有获取积分奖励
        Integer integer = appWorkImgMapper.countUserTodayGrade(workImg.getUserId(), workImg.getCreateTime());
        //当天没有获取积分奖励
        if (null == integer || integer == 0) {
            //最大次数限制
            int max = Integer.parseInt(sysConfigService.selectConfigByKey("app.workImg.grade.max"));
            //用户已经获取的次数
            int userCount = appWorkImgMapper.countUserGrade(workImg.getUserId());
            if (userCount < max) {
                //发放积分奖励
                int grade = Integer.parseInt(sysConfigService.selectConfigByKey("app.workImg.grade"));
                SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
                String format = s.format(workImg.getCreateTime());
                AppWechatUser wechatUser = wechatUserService.selectAppWechatByUserId(workImg.getUserId());
                AppGradeRecord record = new AppGradeRecord();
                record.setUserId(workImg.getUserId());
                record.setGrade(grade);
                record.setType(0);
                record.setBeforeGrade(wechatUser.getGrade());
                record.setCreateTime(DateUtils.getNowDate());
                record.setRemark(format + "云上掠影活动奖励");
                gradeRecordService.insertAppGradeRecord(record);
                AppWechatUser update = new AppWechatUser();
                update.setId(wechatUser.getId());
                update.setGrade(wechatUser.getGrade() + grade);
                wechatUserService.updateAppWechatUser(update);

                //更新获取状态
                AppWorkImg updateImg = new AppWorkImg();
                updateImg.setId(workImg.getId());
                updateImg.setIsGrade(1);
                appWorkImgMapper.updateAppWorkImg(updateImg);
            }
        }
    }

    /**
     * 批量删除健步风采照片
     *
     * @param ids 需要删除的健步风采照片主键
     * @return 结果
     */
    @Override
    public int deleteAppWorkImgByIds(Long[] ids) {
        return appWorkImgMapper.deleteAppWorkImgByIds(ids);
    }

    /**
     * 删除健步风采照片信息
     *
     * @param id 健步风采照片主键
     * @return 结果
     */
    @Override
    public int deleteAppWorkImgById(Long id) {
        return appWorkImgMapper.deleteAppWorkImgById(id);
    }
}
